package cn.mynb.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.mynb.entity.UserBean;
import cn.mynb.dao.UserDao;
import cn.mynb.dao.impl.UserDaoImpl;
import cn.mynb.service.UserService;
import cn.mynb.util.PageModel;

public class UserServiceImpl implements UserService {
	
	private UserDao userdao;
	
	public UserServiceImpl(){
		this.userdao = new UserDaoImpl();
	}

	public int deleteUserService(UserBean user) {
		int ref= 0;	
		try{
			ref = this.userdao.deleteDao(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		return ref;
	}

	@SuppressWarnings("unchecked")
	public List queryAllUserService() {
		List userlist = null;
		String hql =" from UserBean";
		try{
		   userlist = this.userdao.queryAllDao(hql);
		}catch(Exception e){
			e.printStackTrace();
		}
		return userlist;
	}

	public UserBean queryByIdUserService(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public PageModel queryByParamUserService(String hql, String countHQL,
			List param, int offset, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public Serializable saveUserSerializableService(UserBean user) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public int saveUserService(UserBean user) {
		int result =0;
		try{
			result = this.userdao.saveDao(user);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	public int updateUserService(UserBean User) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserBean querySingleUserService(String[] param) {
		String hql ="";
		List list = new ArrayList();
		UserBean user = null;
		if(null != param){
			if(param[0] == "username"){
				hql ="from UserBean where username=?";
				list.add(param[1]);
			}else if("useremail" == param[0]){
				hql = "from UserBean where useremail=?";
				list.add(param[1]);
			}
		}	
		System.out.println("hql:"+hql);
		try{
			user = this.userdao.querySingleDao(hql, list);
		}catch(Exception e){
			e.printStackTrace();
		}
		return user;
	}
	
}
